package com.ll.menu.user.cart;

import android.graphics.Paint;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.droid.library.widget.recyclerview.BaseViewHolder;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;

import org.greenrobot.eventbus.EventBus;

/**
 * 购物车Adapter
 *
 * @author wangheng
 */

public class CartAdapter extends BaseRecyclerAdapter<Food> {


    public CartAdapter(Fragment fragment){
        super(fragment);
    }

    @Override
    protected BaseViewHolder onCreateRealViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.item_cart,parent,false);
        return new Holder(mFragment,view,CartAdapter.this);
    }

    static class Holder extends BaseViewHolder<Food>{
        TextView countDownTextView;
        TextView nameTextView;
        TextView currentPriceTextView;
        TextView oldPriceTextView;
        TextView numberTextView;
        View minusView;
        View plusView;
        View deleteView;


        private String mCurrentPriceMode = null;
        private String mOldPriceMode = null;


        public Holder(Fragment fragment,View itemView,CartAdapter adapter) {
            super(itemView,fragment,adapter);


            mCurrentPriceMode = App.getInstance().getString(R.string.cart_current_price_mode);
            mOldPriceMode = App.getInstance().getString(R.string.cart_old_price_mode);

            countDownTextView = (TextView) itemView.findViewById(R.id.tvCartCountDown);
            nameTextView = (TextView) itemView.findViewById(R.id.tvCartFoodName);
            currentPriceTextView = (TextView) itemView.findViewById(R.id.tvCartCurrentPrice);
            oldPriceTextView = (TextView) itemView.findViewById(R.id.tvCartOldPrice);
            numberTextView = (TextView) itemView.findViewById(R.id.tvCartNumber);

            minusView = itemView.findViewById(R.id.ivCartMinus);
            plusView = itemView.findViewById(R.id.ivCartPlus);
            deleteView = itemView.findViewById(R.id.ivCartDelete);

        }

        @Override
        public void onBindViewHolder(int position, Food item) {

            // 倒计时
            if(item.getCountDown() == 0){
                countDownTextView.setVisibility(View.GONE);
            }else {
                countDownTextView.setVisibility(View.VISIBLE);
                countDownTextView.setText(String.valueOf(item.getCountDown()));
            }
            // 名称
            nameTextView.setText(item.getName());

            // 现价
            currentPriceTextView.setText(String.format(mCurrentPriceMode,item.getCurrentPrice()));

            // 原价
            if(item.getOldPrice() != item.getCurrentPrice()) {
                oldPriceTextView.setVisibility(View.VISIBLE);
                oldPriceTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                oldPriceTextView.setText(String.format(mOldPriceMode, item.getOldPrice()));
            }else{
                oldPriceTextView.setVisibility(View.GONE);
            }

            // 数量
            numberTextView.setText(String.valueOf(item.getCount()));

            // +
            plusView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Food current = mAdapter.getItem(getAdapterPosition());
                    int count = current.getCount();
                    current.setCount(count + 1);
                    numberTextView.setText(String.valueOf(current.getCount()));
                    EventBus.getDefault().post(CartEvent.createEvent(CartEvent.TYPE_CHANGE_FOOD_NUMBER));
                }
            });

            // -
            minusView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Food current = mAdapter.getItem(getAdapterPosition());
                    int count = current.getCount();
                    if(count > 1){
                        current.setCount(count - 1);
                        numberTextView.setText(String.valueOf(current.getCount()));
                        EventBus.getDefault().post(CartEvent.createEvent(CartEvent.TYPE_CHANGE_FOOD_NUMBER));
                    }
                }
            });

            // delete
            deleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAdapter.removeItemAt(getAdapterPosition());
                    EventBus.getDefault().post(CartEvent.createEvent(CartEvent.TYPE_REMOVE_FOOD));
                }
            });
        }
    }
}
